package com.fall.myuvcdemo;

import android.Manifest;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.SurfaceTexture;
import android.os.Bundle;
import android.util.Log;
import android.view.Surface;
import android.view.TextureView;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

import com.fall.uvcfallutils.constants.MyConstants;
import com.fall.uvcfallutils.module.UsbCameraModule;
import com.fall.uvcfallutils.view.AutoFitTextureView;

public class MainActivity extends AppCompatActivity {

    private AutoFitTextureView mTextureView;
    private Button mBtnCapture;
    private ImageView iv_show;

    private UsbCameraModule mUsbCameraModule;
    private Surface mPreviewSurface;

    public UsbCameraModule.onUsbPicListener mUsbPicListener = new UsbCameraModule.onUsbPicListener() {
        @Override
        public void onUsbPic(Bitmap bitmap, String bitmapFileAbsolutePath) {
            if (bitmap != null) {
                iv_show.setImageBitmap(bitmap);
                Log.e("path", "path==>>>" + bitmapFileAbsolutePath + "");
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initUsbCameraModule();

        if (!hasPermissionsGranted(new String[]{
                Manifest.permission.CAMERA, Manifest.permission.RECORD_AUDIO})) {
            requestPermissions(new String[]{Manifest.permission.CAMERA, Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS, Manifest.permission.SYSTEM_ALERT_WINDOW, Manifest.permission.WRITE_EXTERNAL_STORAGE
                    , Manifest.permission.READ_EXTERNAL_STORAGE}, 1);
            return;
        }
    }

    private void initView() {
        mTextureView = (AutoFitTextureView) findViewById(R.id.textureView);
        mTextureView.setSurfaceTextureListener(mSurfaceTextureListener);
        iv_show = findViewById(R.id.iv_show);
        mBtnCapture = findViewById(R.id.btn_capture);
        mBtnCapture.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                capture();
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();

        registerUsbMonitot(true);
        if (mUsbCameraModule != null) {
            if (mPreviewSurface != null && mPreviewSurface.isValid()) {
                startPreview(mPreviewSurface);
            }
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        stopPreview();
        registerUsbMonitot(false);
    }

    private void initUsbCameraModule() {
        if (mUsbCameraModule == null) {
            mUsbCameraModule = UsbCameraModule.getInstance(getApplicationContext());
            mUsbCameraModule.init(MyConstants.DEFAULT_USB_CAM_WIDTH, MyConstants.DEFAULT_USB_CAM_HEIGHT);
        }
    }

    private void registerUsbMonitot(boolean register) {
        if (mUsbCameraModule != null) {
            if (register) {
                mUsbCameraModule.registerUsbMonitor(null, true);
            } else {
                mUsbCameraModule.registerUsbMonitor(null, false);
            }
        }
    }

    private void capture() {
        if (mUsbCameraModule != null) {
            mUsbCameraModule.capture(mUsbPicListener, MainActivity.this);
        }
    }

    TextureView.SurfaceTextureListener mSurfaceTextureListener = new TextureView.SurfaceTextureListener() {
        @Override
        public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
            mPreviewSurface = new Surface(surface);
            startPreview(mPreviewSurface);
        }

        @Override
        public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
        }

        @Override
        public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
            return true;
        }

        @Override
        public void onSurfaceTextureUpdated(SurfaceTexture surface) {

        }
    };

    /**
     * Start Preview
     *
     * @param surface
     */
    private void startPreview(Surface surface) {
        if (mUsbCameraModule != null) {
            mUsbCameraModule.startPreview(surface);
        }
    }

    /**
     * Stop Preview
     */
    private void stopPreview() {
        if (mUsbCameraModule != null) {
            mUsbCameraModule.stopPreview();
        }
    }

    private boolean hasPermissionsGranted(String[] permissions) {
        for (String permission : permissions) {
            if (ActivityCompat.checkSelfPermission(getApplicationContext(), permission)
                    != PackageManager.PERMISSION_GRANTED) {
                return false;
            }
        }
        return true;
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
                                           @NonNull int[] grantResults) {
    }
}